package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.pojo.R;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * Created by ZhaoJun on 2021/11/21 20:09
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService service;
    @Autowired
    private HttpServletRequest request;
    //后台用户登录
    @PostMapping("/login")
    public R login(@RequestBody Employee employee){
//        ①. 将页面提交的密码password进行md5加密处理, 得到加密后的字符串
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());

//        ②. 根据页面提交的用户名username查询数据库中员工数据信息
        String username = employee.getUsername();
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,username);
        Employee one = service.getOne(lqw);
//        ③. 如果没有查询到, 则返回登录失败结果
        if(one == null){
            return R.error("该用户不存在");
        }
//        ④. 密码比对，如果不一致, 则返回登录失败结果
        if(!one.getPassword().equals(password)){
            return R.error("登录失败");
        }
//        ⑤. 查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if(one.getStatus() == 0){
            return R.error("账号已禁用");
        }
//        ⑥. 登录成功，将员工id存入Session, 并返回登录成功结果
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);






    }
    //后台用户登出
    @PostMapping("/logout")
    public R logout(){
//        R<Employee> r = new R<>();
//        r.setCode(1);
//        return r;
        //清理Session中保存的当前登录员工的id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
    //添加后台用户
    @PostMapping
    public R save(@RequestBody Employee employee){
        //用全局异常处理去处理了
//        //查看当前用户名是否存在
//        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(Employee::getUsername,employee.getUsername());
//
//        Employee one = service.getOne(lqw);
//        if(one!=null){
//            return R.error("添加失败,用户名已存在");
//        }
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        //
        Long userId = (Long)request.getSession().getAttribute("employee");
//        employee.setCreateUser(userId);
//        employee.setUpdateUser(userId);
        //产生默认密码并加密赋值
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);

        service.save(employee);

        return R.success("添加成功");
    }
    //查询所有通过指定页及关系
    @GetMapping("/page")
    public R page(Integer page,Integer pageSize,String name){

        Page pageInfo = new Page(page,pageSize);
        System.out.println(pageInfo.getRecords());
        System.out.println("---");


        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);

        lqw.orderByDesc(Employee::getCreateTime);

        service.page(pageInfo, lqw);
        System.out.println(pageInfo.getRecords());

        return R.success(pageInfo);

    }
    //启禁用、修改
    @PutMapping
    public R update(@RequestBody Employee employee){
        if(employee == null){
            return R.error("用户不存在");
        }
        LocalDateTime now = LocalDateTime.now();
        Long id = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(now);
//        employee.setUpdateUser(id);

        System.out.println(employee);
        service.updateById(employee);

        return R.success("修改成功");
    }
    //返现单个员工信息
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        log.info("根据id查询员工信息...");
        Employee one = service.getById(id);
        if(one == null){
            return R.error("该用户不存在");
        }
        return R.success(one);
    }



    

}
